﻿using System;
using System.IO;
using System.Reflection;
using System.Text;
using System.Web;
using Framework.Library.Core.Utils.GlobalHelper;
using Framework.Library.Core.Data.DataManager;
using Framework.Library.Core.Data.DataProvider;
using System.Configuration;
using System.Collections.Generic;

namespace Framework.Library.Core.Config
{    
    public class Configurations
    {
        public static string FRAMEWORK
        {
            get
            {
                return GetStringSetting("FRAMEWORK", "NightStalker");
            }
        }

        public static int CacheExpire
        {
            get
            {
                return GetIntSetting("CACHETIME", 12);
            }
        }

        public static string CachePath
        {
            get
            {
                return GetStringSetting("CACHEPATH", "/Resource/Caches/Dependency/");
            }
        }

        public static bool Cache
        {
            get
            {
                return GetBoolSetting("CACHE", true);
            }
        }

        public static string CacheName
        {
            get
            {
                return GetStringSetting("CACHENAME", "MEMORYCACHE");
            }
        }

        public static bool MULTILANGUAGE
        {
            get
            {
                return GetBoolSetting("MULTILANGUAGE", true);
            }
        }

        public static string SSO_KEY_GENERATE_URL
        {
            get
            {
                return GetStringSetting("SSO_KEY_GENERATE_URL", "http://117.103.196.4/accounts/sso/KeyGenerate.asmx");
            }
        }
        public static string SSO_LOGIN_URL
        {
            get
            {
                return GetStringSetting("SSO_LOGIN_URL", "https://go.vn/accounts/account.login.aspx");
            }
        }
        public static string SSO_LOGOUT_URL
        {
            get
            {
                return GetStringSetting("SSO_LOGOUT_URL", "https://go.vn/accounts/account.logout.aspx");
            }
        }
        public static int SSO_SERVICE_ID
        {
            get
            {
                return GetIntSetting("SSO_SERVICE_ID", 100002);
            }
        }
        public static bool DEBUG_MODE
        {
            get
            {
                return GetBoolSetting("DEBUG_MODE", true);
            }
        }
        public static bool AUTHENTICATION_MODE
        {
            get
            {
                return GetBoolSetting("AUTHENTICATION_MODE", true);
            }
        }
        public static string DATAPROVIDER
        {
            get
            {
                return GetStringSetting("DATAPROVIDER", "SqlServer");
            }
        }
        public static string DATABASEOWNER
        {
            get
            {
                return GetStringSetting("DATABASEOWNER", "NS_");
            }
        }        
        public static string CONNECTIONSTRING
        {
            get
            {
                return GetStringSetting("CONNECTIONSTRING", "Server=(local)\\SQLEXPRESS;Database=Teenio;User ID=sa;Password=123;");
            }
        }
        public static string DBPREFIX
        {
            get
            {
                switch (DATAPROVIDER)
                {
                    case "SqlServer":
                        return "@";
                    case "MySql":
                        return "?";
                    case "Oracle":
                        return ":";
                    case "SQLite":
                        return "@";
                    default:
                        return "@";
                }
            }
        }

        public static DBManager DBMANAGER
        {
            get
            {
                switch (DATAPROVIDER)
                {
                    case "SqlServer":
                        return new MSSQLDBManager();
                    case "MySql":
                        return new MSSQLDBManager();
                    case "Oracle":
                        return new OracleDBManager();
                    default:
                        return new MSSQLDBManager();
                }
                //Chức năng này để kiểm tra kết nối, nếu không thể kết nối sẽ tự động sử dụng SQLite thay thế
                //Tạm thời chưa sử dụng
                //if (DbManager.GetType() != typeof (SQLiteDBManager) && !DbManager.CheckConnection())
                //{
                //    DbManager = new SQLiteDBManager();
                //}
            }
        }
        public static int PORTALID
        {
            get
            {
                int portalId;
                int.TryParse(HttpContext.Current.Request.QueryString["p"], out portalId);
                return (portalId > 0) ? portalId : GetIntSetting("PORTALID", 1);
            }
        }

        public static int LANGID
        {
            get
            {
                int langId;
                int.TryParse(HttpContext.Current.Request.QueryString["l"], out langId);
                return (langId > 0) ? langId : GetIntSetting("LANGID", 2);
            }
        }
        public static string ROOTDIR
        {
            get
            {
                return GetStringSetting("ROOTDIR", "/");
            }
        }
        public static string ROOTPATH
        {
            get
            {
                return GetStringSetting("ROOTPATH", "http://localhost:8800");
            }
        }
        
        public static string Build
        {
            get
            {
                Assembly myAssembly = Assembly.GetExecutingAssembly();
                AssemblyName myAssemblyName = myAssembly.GetName();
                return myAssemblyName.Version.Revision.ToString();
            }
        }
        //Mã giao dịch của đối tác
        public static string TransID
        {
            get
            {
                return GetStringSetting("TRANSID", "0000000413");
            }
        }
        public static string IsEbankTest
        {
            get
            {
                return GetStringSetting("EBANKTEST", "&isTest=1");
            }
        }
        public static string ClientID
        {
            get
            {
                if(!string.IsNullOrEmpty(IsEbankTest))
                    return "k7l6Afre91doSRXeByGymDjNdsI3jVqtb67JZdxito";
                else
                    return GetStringSetting("CLIENTID", "k7l6Afre91doSRXeByGymDjNdsI3jVqtb67JZdxito");
            }
        }
        public static string ClientSecret
        {
            get
            {
                if(!string.IsNullOrEmpty(IsEbankTest))
                    return "6GfeTj9y0CI3Af5O29k0vxniC28t0hYT6bpsg8WrFmQRbtVxgSeKzGqi3mRrRsUTirZvbXaw37hXYagrucEg";
                else
                    return GetStringSetting("CLIENTSECRET", "6GfeTj9y0CI3Af5O29k0vxniC28t0hYT6bpsg8WrFmQRbtVxgSeKzGqi3mRrRsUTirZvbXaw37hXYagrucEg");
            }
        }
        public static string serviceAppId
        {
            get
            {
                if(!string.IsNullOrEmpty(IsEbankTest))
                    return "TEx3b2MvdWYxSmY1aFlobXRiZ3cwUT09";
                else
                    return GetStringSetting("SERVICEAPPID", "TEx3b2MvdWYxSmY1aFlobXRiZ3cwUT09");
            }
        }
            
        public static string serviceKey
        {
            get
            {
                if(!string.IsNullOrEmpty(IsEbankTest))
                    return "14ee22eaba297944c96afdbe5b16c65b";
                else
                    return GetStringSetting("SERVICEKEY", "14ee22eaba297944c96afdbe5b16c65b");
            }
        }
            
        public static string md5key
        {
            get
            {
                if(!string.IsNullOrEmpty(IsEbankTest))
                    return "123456";
                else
                    return GetStringSetting("MD5KEY", "123456");
            }
        }

        public static string makercode
        {
            get
            {
                if(!string.IsNullOrEmpty(IsEbankTest))
                    return "PTCN";
                else
                    return GetStringSetting("MAKERCODE", "PTCN");
            }
        }        

        private static int GetIntSetting(string key, int defaultValue)
        {
            int setting;
            return int.TryParse(ConfigurationManager.AppSettings[key], out setting) ? setting : defaultValue;
        }
        private static bool GetBoolSetting(string key, bool defaultValue)
        {
            if (string.Equals(ConfigurationManager.AppSettings[key], "true", StringComparison.InvariantCultureIgnoreCase)) return true;
            if (string.Equals(ConfigurationManager.AppSettings[key], "false", StringComparison.InvariantCultureIgnoreCase)) return false;
            return defaultValue;
        }
        private static string GetStringSetting(string key, string defaultValue)
        {
            return (ConfigurationManager.AppSettings[key] ?? defaultValue);
        }
    }
}